package com.tianqu.tms.www.statistics.service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.www.statistics.dao.TmsMarketCarAgeCountDao;
import com.tianqu.tms.www.statistics.dto.CarAgeLibraryDto;
import com.tianqu.tms.www.statistics.dto.CarAgeLibraryInnerDto;

public class InventoryCarAgeService {

	
	/**
	 * 车龄统计-在库
	 * @return
	 * @throws TmsDaoException
	 */
	public CarAgeLibraryDto getCarAgeLibrary(String startDate, String endDate) throws TmsDaoException{

		CarAgeLibraryDto carAgeLibraryDto = new CarAgeLibraryDto();

		// 库存平均车价商户TOP5
		TmsMarketCarAgeCountDao tmsMarketCarAgeCountDao = new TmsMarketCarAgeCountDao();
		
		Map<String, CarAgeLibraryInnerDto> carAgeLibrary = tmsMarketCarAgeCountDao.getCarsAgeLibrary(startDate, endDate);
		
		this.between2Date(startDate, endDate, carAgeLibrary, carAgeLibraryDto);
		
		return carAgeLibraryDto;
	}
	
	/**
	 * 两个日期之间的所有日期列表
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	private void between2Date(String startDate, String endDate, Map<String, CarAgeLibraryInnerDto> carAgeLibrary, CarAgeLibraryDto carAgeLibraryDto) {
		
		// 日期列表（X轴）
		ArrayList<String> dateList = new ArrayList<String>();
		
		// 1年以内
		ArrayList<String> ageType1List = new ArrayList<String>();
		
		// 1~2年
		ArrayList<String> ageType2List = new ArrayList<String>();
		
		// 2~3年
		ArrayList<String> ageType3List = new ArrayList<String>();
		
		// 3~4年
		ArrayList<String> ageType4List = new ArrayList<String>();
		
		// 4~5年
		ArrayList<String> ageType5List = new ArrayList<String>();
		
		// 5~6年
		ArrayList<String> ageType6List = new ArrayList<String>();
		
		// 6年以上
		ArrayList<String> ageType7List = new ArrayList<String>();
		
		carAgeInfoByDate(startDate, carAgeLibrary, dateList, ageType1List,ageType2List,ageType3List,ageType4List,ageType5List,ageType6List,ageType7List);
		
		try {
			Date start = DateUtil.string102ToDate(startDate);
			Date end = DateUtil.string102ToDate(endDate);
			

			// 如果start <= end
			while(!DateUtil.compareDate102(start, end)) {
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(start);
				// 加一天
				calendar.add(Calendar.DAY_OF_MONTH, 1);
				
				start = calendar.getTime();
				
				String tmpDate = DateUtil.DateToString11(start);
				
				carAgeInfoByDate(tmpDate, carAgeLibrary, dateList, ageType1List,ageType2List,ageType3List,ageType4List,ageType5List,ageType6List,ageType7List);
			}
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		carAgeLibraryDto.setDateList(dateList);
		carAgeLibraryDto.setAgeType1List(ageType1List);
		carAgeLibraryDto.setAgeType2List(ageType2List);
		carAgeLibraryDto.setAgeType3List(ageType3List);
		carAgeLibraryDto.setAgeType4List(ageType4List);
		carAgeLibraryDto.setAgeType5List(ageType5List);
		carAgeLibraryDto.setAgeType6List(ageType6List);
		carAgeLibraryDto.setAgeType7List(ageType7List);
	}
	
	private void carAgeInfoByDate(String strDate, 
			Map<String, CarAgeLibraryInnerDto> carAgeLibrary,
			ArrayList<String> dateList, 
			ArrayList<String> ageType1List, 
			ArrayList<String> ageType2List,
			ArrayList<String> ageType3List,
			ArrayList<String> ageType4List,
			ArrayList<String> ageType5List,
			ArrayList<String> ageType6List,
			ArrayList<String> ageType7List){
		
		dateList.add(strDate);
		
		CarAgeLibraryInnerDto carAgeLibraryInnerDto1 = carAgeLibrary.get(strDate+"-1");
		if(carAgeLibraryInnerDto1 != null) {
			ageType1List.add(carAgeLibraryInnerDto1.getPriceCount());
		} else {
			ageType1List.add("0");
		}
		
		CarAgeLibraryInnerDto carAgeLibraryInnerDto2 = carAgeLibrary.get(strDate+"-2");
		if(carAgeLibraryInnerDto2 != null) {
			ageType2List.add(carAgeLibraryInnerDto2.getPriceCount());
		} else {
			ageType2List.add("0");
		}
		
		CarAgeLibraryInnerDto carAgeLibraryInnerDto3 = carAgeLibrary.get(strDate+"-3");
		if(carAgeLibraryInnerDto3 != null) {
			ageType3List.add(carAgeLibraryInnerDto3.getPriceCount());
		} else {
			ageType3List.add("0");
		}
		
		CarAgeLibraryInnerDto carAgeLibraryInnerDto4 = carAgeLibrary.get(strDate+"-4");
		if(carAgeLibraryInnerDto4 != null) {
			ageType4List.add(carAgeLibraryInnerDto4.getPriceCount());
		} else {
			ageType4List.add("0");
		}
		
		CarAgeLibraryInnerDto carAgeLibraryInnerDto5 = carAgeLibrary.get(strDate+"-5");
		if(carAgeLibraryInnerDto5 != null) {
			ageType5List.add(carAgeLibraryInnerDto5.getPriceCount());
		} else {
			ageType5List.add("0");
		}
		
		CarAgeLibraryInnerDto carAgeLibraryInnerDto6 = carAgeLibrary.get(strDate+"-6");
		if(carAgeLibraryInnerDto6 != null) {
			ageType6List.add(carAgeLibraryInnerDto6.getPriceCount());
		} else {
			ageType6List.add("0");
		}
		
		CarAgeLibraryInnerDto carAgeLibraryInnerDto7 = carAgeLibrary.get(strDate+"-7");
		if(carAgeLibraryInnerDto7 != null) {
			ageType7List.add(carAgeLibraryInnerDto7.getPriceCount());
		} else {
			ageType7List.add("0");
		}
	}
}
